package com.wangyang.stock.web.servlet;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.wangyang.stock.business.IEmployeeService;
import com.wangyang.stock.business.IEmploymentService;
import com.wangyang.stock.business.IInOutService;
import com.wangyang.stock.business.IInvoiceTypeService;
import com.wangyang.stock.business.IMerchandiseTypeService;
import com.wangyang.stock.business.IMerchandiseUnitService;
import com.wangyang.stock.business.IRoleService;
import com.wangyang.stock.business.IStorehouseService;
import com.wangyang.stock.vo.EmployeeVo;

public class InitServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		
		
		// 得到 applicationContext.xml 的路径
		String url = super.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");
		// 解析 applicationContext.xml 文件
		ApplicationContext context = new FileSystemXmlApplicationContext(url);
		
		IEmployeeService employeeService= (IEmployeeService) context.getBean("employeeService");
		IEmploymentService employmentService= (IEmploymentService) context.getBean("employmentService");
		IInOutService inOutService= (IInOutService) context.getBean("inOutService");
		IStorehouseService storehouseService= (IStorehouseService) context.getBean("storehouseService");
		IInvoiceTypeService invoiceTypeService= (IInvoiceTypeService) context.getBean("invoiceTypeService");
		IMerchandiseTypeService merchandiseTypeService= (IMerchandiseTypeService) context.getBean("merchandiseTypeService");
		IMerchandiseUnitService merchandiseUnitService= (IMerchandiseUnitService) context.getBean("merchandiseUnitService");
		IRoleService roleService= (IRoleService) context.getBean("roleService");
		
		EmployeeVo purchasingEmp = new EmployeeVo();
		purchasingEmp.setEmployeePermission("1");
		EmployeeVo businessEmp = new EmployeeVo();
		purchasingEmp.setEmployeePermission("2");
		EmployeeVo storekeeperEmp = new EmployeeVo();
		purchasingEmp.setEmployeePermission("3");

		List purchasingEmpList = employeeService.selectEmployee(purchasingEmp);
		List businessEmpList = employeeService.selectEmployee(businessEmp);
		List storekeeperEmpList = employeeService.selectEmployee(storekeeperEmp);
		List employmentList = employmentService.selectEmployment(null);
		List inOutList = inOutService.selectInOut(null);
		List storehouseList = storehouseService.selectStorehouse(null);
		List invoiceTypeList = invoiceTypeService.selectInvoiceType(null);
		List merchandiseTypeList = merchandiseTypeService.selectMerchandiseType(null);
		List merchandiseUnitList = merchandiseUnitService.selectMerchandiseUnit(null);
		List roleList = roleService.selectRole(null);

		ServletContext servletContext = super.getServletContext();

		servletContext.setAttribute("purchasingEmpList", purchasingEmpList);
		servletContext.setAttribute("businessEmpList", businessEmpList);
		servletContext.setAttribute("storekeeperEmpList", storekeeperEmpList);
		servletContext.setAttribute("employmentList", employmentList);
		servletContext.setAttribute("inOutList", inOutList);
		servletContext.setAttribute("storehouseList", storehouseList);
		servletContext.setAttribute("invoiceTypeList", invoiceTypeList);
		servletContext.setAttribute("merchandiseTypeList", merchandiseTypeList);
		servletContext.setAttribute("merchandiseUnitList", merchandiseUnitList);
		servletContext.setAttribute("roleList", roleList);
	}

}
